package com.bw.liuyushan.zhuangao4_weektest2.MVP.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bw.liuyushan.mvp_lib.View.BaseActivity;
import com.bw.liuyushan.zhuangao4_weektest2.Adapter.FragmentMainAdapter;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Contrack.IContrackTest;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Fragment.Fragment_Main1;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Fragment.Fragment_Main2;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Fragment.Fragment_Main3;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Fragment.Fragment_Main4;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Fragment.Fragment_Main5;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Model.BaseModelTest;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Presenter.BasePresenterTest;
import com.bw.liuyushan.zhuangao4_weektest2.R;

import java.util.ArrayList;

public class MainActivity extends BaseActivity<BasePresenterTest> implements IContrackTest.C_ViewTest {
    private androidx.viewpager.widget.ViewPager vpMain;
    private android.widget.RadioGroup rgMain;
    private android.widget.RadioButton rgbtn1;
    private android.widget.RadioButton rgbtn2;
    private android.widget.RadioButton rgbtn3;
    private android.widget.RadioButton rgbtn4;
    private android.widget.RadioButton rgbtn5;
    ArrayList<Fragment> fragments;

    @Override
    public int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        //防止内存泄露
        ip=new BasePresenterTest(new BaseModelTest(),this);

        vpMain = (ViewPager) findViewById(R.id.vp_Main);
        rgMain = (RadioGroup) findViewById(R.id.rg_Main);
        rgbtn1 = (RadioButton) findViewById(R.id.rgbtn1);
        rgbtn2 = (RadioButton) findViewById(R.id.rgbtn2);
        rgbtn3 = (RadioButton) findViewById(R.id.rgbtn3);
        rgbtn4 = (RadioButton) findViewById(R.id.rgbtn4);
        rgbtn5 = (RadioButton) findViewById(R.id.rgbtn5);
    }

    @Override
    public void initDate() {


        fragments = new ArrayList<>();
        fragments.add(new Fragment_Main1());
        fragments.add(new Fragment_Main2());
        fragments.add(new Fragment_Main3());
        fragments.add(new Fragment_Main4());
        fragments.add(new Fragment_Main5());

        FragmentMainAdapter fragmentMainAdapter = new FragmentMainAdapter(getSupportFragmentManager(), fragments);
        vpMain.setAdapter(fragmentMainAdapter);

        rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rgbtn1:
                        vpMain.setCurrentItem(0);
                        break;
                    case R.id.rgbtn2:
                        vpMain.setCurrentItem(1);
                        break;
                    case R.id.rgbtn3:
                        vpMain.setCurrentItem(2);
                        break;
                    case R.id.rgbtn4:
                        vpMain.setCurrentItem(3);
                        break;
                    case R.id.rgbtn5:
                        vpMain.setCurrentItem(4);
                        break;

                }
            }
        });
        vpMain.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        rgMain.check(R.id.rgbtn1);
                        break;
                    case 1:
                        rgMain.check(R.id.rgbtn2);
                        break;
                    case 2:
                        rgMain.check(R.id.rgbtn3);
                        break;
                    case 3:
                        rgMain.check(R.id.rgbtn4);
                        break;
                    case 4:
                        rgMain.check(R.id.rgbtn5);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}
